var pageNum=1,pageSize=5,pageTotal=0;
$(function(){
	loadAllApplicantMessage();
});

function loadAllApplicantMessage(){
	utilAjax("/basic_information/queryAllBasic_information","pageNum="+pageNum+"&pageSize="+pageSize,"get",
			function(data){
		pageNum = data.pageNum;
		pageSize = data.pageSize;
		pageTotal = data.pages;
		parseAllApplicantMessage(data.list);
	});
}

function parseAllApplicantMessage(lists){
	var appCon = "<tr class='info text-info'><td>编号</td><td>头像</td><td>昵称</td><td>邮箱</td><td>手机</td><td>身份证</td><td>密码</td><td>求职状态</td><td>登陆状态</td><td>创建时间</td><td>操作</td></tr>";
	if(lists!=""){
		$.each(lists,function(i,a){
			var login_state = "";
			if(a.basic_login_state==1){login_state= "<input type='button'  class='btn btn-primary' value='禁止登录' onclick=updateLogin_state("+a.basic_id+",2)>"}
			if(a.basic_login_state==2){login_state= "<input type='button'  class='btn btn-primary' value='正常登录' onclick=updateLogin_state("+a.basic_id+",1)>"}
			appCon += "<tr>"+
						"<td>"+a.basic_id+"</td>"+
						"<td><img src='/image/"+a.basic_photo+"' width='45' height='45' class='img-thumbnail'></td>"+
						"<td>"+a.basic_name+"</td>"+
						"<td>"+a.basic_email+"</td>"+
						"<td>"+a.basic_phone+"</td>"+
						"<td>"+a.basic_card+"</td>"+
						"<td>"+a.basic_password+"</td>"+
						"<td>"+(a.basic_job_state==1?"不接收本地区招聘公司的个人信息浏览":"接收本地区招聘公司的个人信息浏览")+"</td>"+
						"<td>"+(a.basic_login_state==1?"正常登录":"<font color='red'>禁止登录</font>")+"</td>"+
						"<td>"+dateUtil(a.create_time)+"</td>"+
						"<td>"+login_state+"</td>"+
					"</tr>";
		});
		appCon+= "<tr><td colspan='11' align='center'>当前页:"+pageNum+"/总页数:"+pageTotal+"</td></tr>";
		appCon+= "<tr><td colspan='11' align='center'>" +
				"<input type='button' value='首页'  class='btn btn-primary' onclick=changeApplicantPage(this)> " +
				"<input type='button' value='上一页'  class='btn btn-primary' onclick=changeApplicantPage(this)> " +
				"<input type='button' value='下一页'  class='btn btn-primary' onclick=changeApplicantPage(this)> " +
				"<input type='button' value='末页'  class='btn btn-primary' onclick=changeApplicantPage(this)> " +
			"</td></tr>";
	}else{
		appCon+= "<tr><td colspan='9' align='center'><font size='6' color='red'>暂无应聘者</font></td></tr>";
	}
	
	$("#applicantTable").html(appCon);
}

function updateLogin_state(basic_id,login_state){
	utilAjax("/basic_information/updateBasic_login_state","basic_id="+basic_id+"&login_state="+login_state,"post",
			function(flag){
		if(flag){
			alert("修改成功");
			loadAllApplicantMessage();
		}else{
		}
	});
}

function changeApplicantPage(op){
	var butCon = $(op).val();
	switch(butCon){
	case "首页":
		pageNum=1;
		loadAllApplicantMessage();
		break;
	case "上一页":
		if(pageNum>1){
			pageNum--;
			loadAllApplicantMessage();
		}	
		break;
	case "下一页":
		if(pageNum<pageTotal){
			pageNum++;
			loadAllApplicantMessage();
		}
		break;
	case "末页":
		pageNum=pageTotal;
		loadAllApplicantMessage();
		break;
	}
}

function utilAjax(url,data,type,success){
	$.ajax({
		url:url,
		data:data,
		dataType:'json',
		type:type,
		success:success,
		error:function(){
		}
	});
}

function dateUtil(times){
	var date = new Date(times);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	Y = date.getFullYear() + '-';
	M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
			.getMonth() + 1)
			+ '-';
	D = date.getDate() + ' ';
	var dateCon = Y + M + D;
	return dateCon;
}